package test.serverframe.armc.server.manager.dao.mapper;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import test.serverframe.armc.server.manager.domain.TaskEnterInvoke;

import java.util.List;

public interface TaskEnterInvokeMapper {
    @Delete({
            "delete from tb_ssgp_task_enter_invoke",
            "where task_enter_invoke_id = #{taskEnterInvokeId,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(String taskEnterInvokeId);

    @Delete({
            "delete from tb_ssgp_task_enter_invoke",
            "where task_invoke_id = #{taskInvokeId,jdbcType=VARCHAR}"
    })
    int deleteByTaskInvokeId(String taskInvokeId);

    @Insert({
            "insert into tb_ssgp_task_enter_invoke (task_enter_invoke_id, task_id, ",
            "task_invoke_id, task_delay, task_parent_ids, task_child_ids)",
            "values (#{taskEnterInvokeId,jdbcType=VARCHAR}, #{taskId,jdbcType=VARCHAR}, ",
            "#{taskInvokeId,jdbcType=VARCHAR}, #{taskDelay,jdbcType=INTEGER},",
            "#{taskParentIds,jdbcType=VARCHAR}, #{taskChildIds,jdbcType=VARCHAR}"
    })
    int insert(TaskEnterInvoke record);

    @InsertProvider(type = TaskEnterInvokeSqlProvider.class, method = "insertSelective")
    int insertSelective(TaskEnterInvoke record);


    @Insert({
            "<script>",
            "insert into tb_ssgp_task_enter_invoke(task_enter_invoke_id, task_id, task_invoke_id, ",
            "task_delay, task_parent_ids, task_child_ids) values ",
            "<foreach collection='taskEnterInvokes' item='item' index='index' separator=','>",
            "(#{item.taskEnterInvokeId}, #{item.taskId}, #{item.taskInvokeId}, #{item.taskDelay},",
            "#{item.taskParentIds}, #{item.taskChildIds})",
            "</foreach>",
            "</script>"
    })
    int insertCollectList(@Param("taskEnterInvokes") List<TaskEnterInvoke> taskEnterInvokes);

    @Select({
            "select",
            "task_enter_invoke_id, task_id, task_invoke_id, task_delay, task_parent_ids, task_child_ids",
            "from tb_ssgp_task_enter_invoke",
            "where task_enter_invoke_id = #{taskEnterInvokeId,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column = "task_enter_invoke_id", property = "taskEnterInvokeId", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "task_id", property = "taskId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "task_invoke_id", property = "taskInvokeId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "task_delay", property = "taskDelay", jdbcType = JdbcType.INTEGER),
            @Result(column = "task_parent_ids", property = "taskParentIds", jdbcType = JdbcType.VARCHAR),
            @Result(column = "task_child_ids", property = "taskChildIds", jdbcType = JdbcType.VARCHAR),
    })
    TaskEnterInvoke selectByPrimaryKey(String taskEnterInvokeId);

    @UpdateProvider(type = TaskEnterInvokeSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(TaskEnterInvoke record);

    @Update({
            "update tb_ssgp_task_enter_invoke",
            "set task_id = #{taskId,jdbcType=VARCHAR},",
            "task_invoke_id = #{taskInvokeId,jdbcType=VARCHAR}",
            "task_delay = #{taskDelay,jdbcType=INTEGER}",
            "task_parent_ids = #{taskParentIds,jdbcType=VARCHAR}",
            "task_child_ids = #{taskChildIds,jdbcType=VARCHAR}",
            "where task_enter_invoke_id = #{taskEnterInvokeId,jdbcType=VARCHAR}"
    })
    int updateByPrimaryKey(TaskEnterInvoke record);

    @Select({
            "select",
            "task_enter_invoke_id, task_id, task_invoke_id, task_delay, task_parent_ids, task_child_ids",
            "from tb_ssgp_task_enter_invoke",
            "where task_invoke_id = #{taskInvokeId,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column = "task_enter_invoke_id", property = "taskEnterInvokeId", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "task_id", property = "taskId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "task_invoke_id", property = "taskInvokeId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "task_delay", property = "taskDelay", jdbcType = JdbcType.INTEGER),
            @Result(column = "task_parent_ids", property = "taskParentIds", jdbcType = JdbcType.VARCHAR),
            @Result(column = "task_child_ids", property = "taskChildIds", jdbcType = JdbcType.VARCHAR),
            @Result(column = "task_id", property = "task", one = @One(
                    select = "test.serverframe.armc.server.manager.dao.mapper.TaskMapper.selectByPrimaryKey"
            ))
    })
    List<TaskEnterInvoke> selectByTaskInvokeId(String taskInvokeId);
    
    @Select({
	    "SELECT task_enter_invoke_id,task_id, task_invoke_id, task_delay",
	    "FROM (",
	    	"select  tb.task_id, tb.task_invoke_id, tb.task_delay, task.version,task.task_group_id, tb.task_enter_invoke_id",
	    	"from (",
	    		"SELECT task_enter_invoke_id,task_id, task_invoke_id, task_delay ",
	    		"from tb_ssgp_task_enter_invoke",
	    		"where task_invoke_id = #{taskInvokeId,jdbcType=VARCHAR}",
	    		") as tb INNER JOIN tb_ssgp_task as task ON task.task_id = tb.task_id",
	    	"order by task.version DESC) AS newtb",
	    "group by newtb.task_group_id",
//	    "order by newtb.task_order asc",
	    
    })
    @Results({
        @Result(column = "task_enter_invoke_id", property = "taskEnterInvokeId", jdbcType = JdbcType.VARCHAR, id = true),
        @Result(column = "task_id", property = "taskId", jdbcType = JdbcType.VARCHAR),
        @Result(column = "task_invoke_id", property = "taskInvokeId", jdbcType = JdbcType.VARCHAR),
        @Result(column = "task_delay", property = "taskDelay", jdbcType = JdbcType.INTEGER),
        @Result(column = "task_parent_ids", property = "taskParentIds", jdbcType = JdbcType.VARCHAR),
        @Result(column = "task_child_ids", property = "taskChildIds", jdbcType = JdbcType.VARCHAR),
    })
    List<TaskEnterInvoke> selectTaskIdByInvokeId(String taskInvokeId);
    
    @Select({
        "select",
        "task_enter_invoke_id, task_id, task_invoke_id, task_delay,task_parent_ids, task_child_ids",
        "from tb_ssgp_task_enter_invoke",
        "where task_id = #{taskId,jdbcType=VARCHAR}"
	})
	@Results({
	        @Result(column = "task_enter_invoke_id", property = "taskEnterInvokeId", jdbcType = JdbcType.VARCHAR, id = true),
			@Result(column = "task_id", property = "taskId", jdbcType = JdbcType.VARCHAR),
			@Result(column = "task_invoke_id", property = "taskInvokeId", jdbcType = JdbcType.VARCHAR),
			@Result(column = "task_delay", property = "taskDelay", jdbcType = JdbcType.INTEGER),
			@Result(column = "task_parent_ids", property = "taskParentIds", jdbcType = JdbcType.VARCHAR),
            @Result(column = "task_child_ids", property = "taskChildIds", jdbcType = JdbcType.VARCHAR),
	})
	List<TaskEnterInvoke> selectByTaskId(String taskId);

    @Select({
        "select task_delay",
        "from tb_ssgp_task_enter_invoke",
        "where task_id = #{taskId,jdbcType=VARCHAR} and task_invoke_id = #{taskInvokeId,jdbcType=VARCHAR}"
	})
	List<Integer> findDelay(TaskEnterInvoke tei);

}